package com.toby.demo.dldemo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import DependencyLanguage.DLException;
import DependencyLanguage.Engine;
import DependencyLanguage.Variable;
import DependencyLanguage.Variables;

public class DLScriptHandler {

	public static void main(String[] args) {
		DLScriptHandler handler = new DLScriptHandler();
		DLScript dlScript = new DLScript();
		String[] inputVars = new String[] { "iMic", "iId", "iTimeStamp",
				"iBidsString", "iOffersString", "iCITIBidRow", "iCITIOfferRow" };
		String[] outputVars = new String[] { "citiEHBidPrice",
				"citiEHOfferPrice", "citiBidSize", "citiOfferSize" };
		dlScript.setDlInputVars(inputVars);
		dlScript.setDlOutputVars(outputVars);
		try {
			String dlScriptContents = handler.getDLScriptFileContents("CITIEnhancedPrice.dl");
			dlScript.setGraph(Engine.Parse(dlScriptContents));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, Object> msg = new HashMap<String, Object>();
		msg.put("iMic", "aaa");
		msg.put("iId", "aaa");
		msg.put("iTimeStamp", "2015-01-14 16:42:00");
		msg.put("iBidsString", "CITI123456");
		msg.put("iOffersString", "CITI456789");
		msg.put("iCITIBidRow", "333333");
		msg.put("iCITIOfferRow", "8333333");
		for (int i = 0; i < outputVars.length; i++) {
			System.out.println(msg.get(outputVars[i]));
		}
		handler.runDLScript(msg, dlScript);
		for (int i = 0; i < outputVars.length; i++) {
			System.out.println(msg.get(outputVars[i]));
		}
	}

	private void runDLScript(Map<String, Object> data, DLScript dlScript) {
		Variables variables = dlScript.getGraph().get_Variables();
		String[] dlInputVarKeys = dlScript.getDlInputVars();
		Variable x = null;
		for (int i = 0; i < dlInputVarKeys.length; i++) {
			x = variables.get_Item(dlInputVarKeys[i]);
			if (x == null) {
				throw new DLException("Input variable " + dlInputVarKeys[i]
						+ " does not exist in DL graph");
			}
			x.Set(data.get(dlInputVarKeys[i]));
		}
		Variable y = null;
		String[] dlOutputVarKeys = dlScript.getDlOutputVars();
		for (int i = 0; i < dlOutputVarKeys.length; i++) {
			y = variables.get_Item(dlOutputVarKeys[i]);
			if (y == null) {
				throw new DLException("Output variable " + dlOutputVarKeys[i]
						+ " does not exist in DL graph");
			}
			data.put(dlOutputVarKeys[i], y.get_VariableValue());
		}
	}

	private String getDLScriptFileContents(String scriptPath)
			throws IOException {
		StringBuffer fileData = new StringBuffer(1000);
		BufferedReader reader = new BufferedReader(new FileReader(this
				.getClass().getClassLoader().getResource(scriptPath).getPath()));
		char[] buf = new char[1024];
		int numRead = 0;
		String readData;
		while ((numRead = reader.read(buf)) != -1) {
			readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
			buf = new char[1024];
		}
		reader.close();
		return fileData.toString();
	}

}
